//
//  HSHomeWorkViewModel.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/10/10.
//  Copyright © 2016年 赖虹宇. All rights reserved.
//

import UIKit



typealias homeworkCallBack = (_ error : Error?,_ isRersh:Bool) -> ()

class HSHomeWorkViewModel {
     //模型数组
    var homeWordArr : [HSHomeWorkModel] = []
    
    //请求数据
    func requestHomeWorkData(finished:@escaping homeworkCallBack){
        //获取接口
        guard  let key = userInfo["key"] else{
            return
        }
        let id = 1
        //接口地址
        let url = "ZH-schoolname-S-getHomeworkBase"
        let parame = [ "key":key ,"id":id]
        
        HSNetwordTools.shared.downloadData(urlStr: url, parameters: parame) { (data, error) in
            guard let data = data as? NSDictionary else{
                HSAlertView.bottomAlertView("网络无连接，请检查网络")
                finished(error!, false)
                return

            }
//            print(data["data"])
            switch data["result"] as! Int {
            case 0 :
                //字典转模型
                //1.获得字典数组
                let dictArray = data["data"] as! [[String : Any]]
                //2.创建一个保存模型的数组
                var modelArray = [HSHomeWorkModel]()
                //遍历取出模型
                for dict in dictArray{
                    let homeWorkModel = HSHomeWorkModel(dict: dict)
                    modelArray.append(homeWorkModel)
                }
                self.homeWordArr = modelArray
                //刷新视图--数据正常处理完成后刷新视图
                finished(nil, true)
            case 1 :
                HSAlertView.bottomAlertView("已经没有更早的数据了")
                //网络正常，但是数据加载失败，通知控制器不需要刷新视图
                finished(nil, false)
            default:
                break
            }
            
        }
        

    }
}
